package easier.fast.page.drag;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import easier.fast.R;
import easier.fast.page.RecyclerPageBridge;
import easier.fast.page.pull.RefreshRecyclerManager;
import easier.recycler.MultiTypeAdapter.BaseViewHolder;
import easier.recycler.MultiTypeAdapter.TypeViewHolder;
import java.util.List;

/**
 * 同时管理{@link SmartRefreshLayout}和{@link RecyclerView}简化操作,支持上拉加载,下拉加载,分页数据,空数据界面,网络异常界面
 *
 * @param <T> 数据类型
 */
public abstract class RefreshRecyclerPageBridge< T > extends RecyclerPageBridge< T > {

    protected SmartRefreshLayout mRefresh;

    public RefreshRecyclerPageBridge( SmartRefreshLayout refreshLayout, RecyclerView recyclerView ) {

        super( recyclerView );
        mRefresh = refreshLayout;

        mRefresh.setOnRefreshListener( new OnRefreshListener() {
            @Override
            public void onRefresh( @NonNull RefreshLayout refreshLayout ) {

                refresh();
            }
        } );

        mRefresh.setOnLoadMoreListener( new OnLoadMoreListener() {
            @Override
            public void onLoadMore( @NonNull RefreshLayout refreshLayout ) {

                loadMore();
            }
        } );
    }

    @Override
    protected void notifyFinish( RecyclerView recyclerView, List< Page< T > > page, int mode, boolean success ) {

        super.notifyFinish( recyclerView, page, mode, success );

        /* 根据数据状态决定行为 */
        if ( mode == 1 ) {
            mRefresh.finishRefresh();
            if ( isRequestAll( page ) ) {
                mRefresh.finishLoadMoreWithNoMoreData();
            } else {
                mRefresh.finishLoadMore();
            }
        } else if ( mode == 2 ) {
            if ( isRequestAll( page ) ) {
                mRefresh.finishLoadMoreWithNoMoreData();
            } else {
                mRefresh.finishLoadMore();
            }
        }

        if ( !success ) {
            mRefresh.finishLoadMoreWithNoMoreData();
        }
    }

    /**
     * 创建后,调用该方法开始获取数据
     */
    public void start() {

        mRefresh.post( new Runnable() {
            @Override
            public void run() {

                mRefresh.autoRefresh();
            }
        } );
    }

    @Override
    protected int configPageSize() {

        return super.configPageSize();
    }

    /**
     * 判断是否已经获取所有数据
     *
     * @param page 分页数据
     *
     * @return true:获取了所有数据{@link SmartRefreshLayout#finishLoadMoreWithNoMoreData()}显示没有更多数据
     */
    private boolean isRequestAll( List< Page< T > > page ) {

        Page< T > tPage = page.get( page.size() - 1 );
        return tPage.getPage() * tPage.getPageSize() >= getTotalCount();
    }

    protected int getTotalCount() {

        return Integer.MAX_VALUE;
    }

    /**
     * @param recyclerView 空数据界面
     *
     * @return 空数据界面
     */
    @NonNull
    @Override
    protected TypeViewHolder configEmptyView( RecyclerView recyclerView ) {

        return new BaseViewHolder( R.layout.common_no_data ) {
            @Override
            public boolean itemEquals( @Nullable TypeViewHolder obj ) {

                if ( obj instanceof BaseViewHolder ) {
                    return ((BaseViewHolder) obj).getType() == R.layout.common_no_data;
                }
                return super.itemEquals( obj );
            }

            @Override
            public boolean contentEquals( @Nullable TypeViewHolder obj ) {

                return true;
            }
        };
    }

    /**
     * @param e 网络异常界面
     *
     * @return 网络异常界面
     */
    @Nullable
    @Override
    protected TypeViewHolder configExceptionView( Throwable e ) {

        if ( RefreshRecyclerManager.isConnectError( e ) ) {
            return new BaseViewHolder( R.layout.common_net_error ) {

                @Override
                public boolean itemEquals( @Nullable TypeViewHolder obj ) {

                    if ( obj instanceof BaseViewHolder ) {
                        return ((BaseViewHolder) obj).getType() == R.layout.common_net_error;
                    }
                    return super.itemEquals( obj );
                }

                @Override
                public boolean contentEquals( @Nullable TypeViewHolder obj ) {

                    return true;
                }
            };
        }
        return super.configExceptionView( e );
    }
}
